<?php
/**
 * @package AMFramework
 * @subpackage Data_Pack
 * @author Marcu Alexandru
 * @version 1.0 $Build 305b  
 */
class Amfw_Html_Form {
	
	public $params = '';
	public $created = false;
	public $elements = array();
	
	private $html = '';
	private $table = false;
	function form($command, $params = array()) {
		switch($command) {
			case 'create':
				$html = '';
				$html .= '<form';
				if(!empty($params)) {
					foreach($params as $key => $value) {
						$html .= ' '.$key.'="'.$value.'"';
					}
				}
				$html .= '>'."\n";
				$this->html .= $html;
				break;
			case 'close':
				$this->html .= "\n".'</form>';
				break;		
		}
	}
	function table($command, $params = array()) {
		switch($command) {
			case 'create':
				$this->table = true;
				$html = '';
				$html .= '<table';
				if(!empty($params)) {
					foreach($params as $paramkey => $paramval) {
						$html .= ' '.$paramkey.'="'.$paramval.'"';
					}
				}
				$html .= '>';
				$this->html .= $html;
				break;
			case 'close':
				$this->html .= "\n".'</table>';
				$this->table = false;
				break;
		}
	}
	/**
	 * 
	 * @param $html_tag	tagul html care va fi deschis
	 * @param $params 	parametri tagului html
	 * @param $value	valoarea din interiorul tagului (if any)
	 * @param $br_after_label daca sa puna sau nu br dupa label;
	 * @param $label	label pentru tag (if any)
	 */
	function addElement($html_tag, $params = array(),$label = null, $br_after_label = false, $value = null) {
		$html = '';
		$html .= $this->table ? '<tr>' : '';
		if(isset($label)&&(!in_array(@$params['type'],array('checkbox','radio')))) {
			$html .= $this->table ? '<td>' : '';
			$html .= '<label for="'.$params['name'].'">'.$label.'</label>';
			$html .= $this->table ? '</td>' : $br_after_label ? '<br/>' : '';
		} else {	// Table is not set;
			$html .= $this->table ? '<td>&nbsp;</td>' : '';
		}
		$html .= $this->table ? '<td>' : '';
		$html .= '<'.$html_tag;
		if(!empty($params)) {
			foreach($params as $paramkey => $paramval) {
				$html .= ' '.$paramkey.'="'.$paramval.'"';
			}
		}
		if(isset($value)) {
			$html .= '>'.$value.'</'.$html_tag.'>';
		}
		else $html .= '/>';
		if(in_array(@$params['type'],array('checkbox','radio'))) {
			$html .= '<label for="'.$params['name'].'">'.$label.'</label>';
		}
		$html .= $this->table ? '</td>' : '';
 		$html .= $this->table ? '</tr>' : '';

 		$this->html .="\n".$html;
	}
	function addCsrfToken() {
		$this->html .= "\n".Amfw_Html::token();
	}
	function getHtml() {
		return $this->html;
	}
	function addScript($js) {
		$this->html .= '<script type="text/javascript">'."\n".$js.'</script>';
	}
	function addHtml($html) {
		$this->html .= $html; 
	}
}

/*<form action="{@form_register_action}" method="post" name="registerForm">
	<table>
		<tr><td><label for="name">{@text_name}</label></td><td><input type="text" name="name" /></td></tr>
		<tr><td><label for="surname">{@text_surname}</label></td><td><input type="text" name="surname" /></td></tr>
		<tr><td><label for="email">{@text_email}</label></td><td><input type="text" name="email" /></td></tr>
		<tr><td>&nbsp;</td><td><input type="button" value="{@text_register_button}" name="registered" /></td></tr>
	</table>
	{@hidden_token}
</form>*/
